package cn.com.zhui.demo.workflow.handler;

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * <p>
 * Mybatis plus 公共字段填充类
 * </p>
 *
 * @author 张辉
 * @since 2021-12-04
 */
@Component
public class MybatisplusMetaObjectHandler extends MetaObjectHandler {
    private static final String COLUMN_NAME_CREATED_AT = "createdAt";
    private static final String COLUMN_NAME_UPDATED_AT = "updatedAt";

    @Override
    public void insertFill(MetaObject metaObject) {
        Date sysDate = new Date();
        if (getFieldValByName(COLUMN_NAME_CREATED_AT, metaObject) == null) {
            setFieldValByName(COLUMN_NAME_CREATED_AT, sysDate, metaObject);
        }
        if (getFieldValByName(COLUMN_NAME_UPDATED_AT, metaObject) == null) {
            setFieldValByName(COLUMN_NAME_UPDATED_AT, sysDate, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName(COLUMN_NAME_UPDATED_AT, new Date(), metaObject);
    }
}
